package com.exam.examtest.util

import android.content.Context
import android.view.Gravity
import android.widget.Toast

/**
 * ClassName ToastUtil
 * Create by rhf
 * Create on 2021/3/30 16:23
 * Description 吐司
 */
class ToastUtil {

    companion object {

        @JvmField
        var mToast: Toast? = null

        @JvmStatic
        fun showToast(context: Context?,msg: String) {
            context?.let { showShortToast(it,msg) }
        }

        @JvmStatic
        fun showCenterToast(context: Context?, msg: String, duration: Int) {
            if (mToast == null && context != null) {
                mToast = Toast.makeText(context, msg, duration)
            } else {
                mToast!!.setText(msg)
                mToast!!.duration = duration // 避免toase重复出现
            }
            mToast!!.setGravity(Gravity.CENTER, 0, 0)
            mToast!!.show()
        }

        /**
         * 短时toast
         * @param context 上下文
         * @param msg 消息体
         */
        @JvmStatic
        fun showShortToast(context: Context, msg: String) {
            if (mToast == null) {
                mToast = Toast.makeText(context, msg, Toast.LENGTH_SHORT)
            } else {
                mToast!!.setText(msg)
                mToast!!.duration = Toast.LENGTH_SHORT// 避免toase重复出现
            }
            // mToast.setGravity(Gravity.CENTER, 0, 0);
            mToast!!.show()
        }

        /**
         * 弹出吐司
         */
        @JvmStatic
        fun showToast(context: Context, msg: String, duration: Int) {
            if (mToast == null) {
                mToast = Toast.makeText(context, msg, duration)
            } else {
                mToast!!.setText(msg)
                mToast!!.duration = duration// 避免toase重复出现
            }
            // mToast.setGravity(Gravity.CENTER, 0, 0);
            mToast!!.show()
        }

        /**
         * 使toast不在显示
         */
        @JvmStatic
        fun cancleMyToast() {
            if (mToast != null) {
                mToast!!.cancel()
            }
        }
    }
}